package umlms.server.conn;

public class DefaultDispatcher implements IDispatcher {

	public int getRequestType(String request) {

		String requestLower = request.toLowerCase();
		if (requestLower.startsWith("fetch")
				|| requestLower.startsWith("stream")) {
			return TYPE_UMLQL;
		} else if (requestLower.startsWith("insert")
				|| requestLower.startsWith("update")
				|| requestLower.startsWith("delete")) {
			return TYPE_UMLML;
		} else if (requestLower.startsWith("create")
				|| requestLower.startsWith("alter")
				|| requestLower.startsWith("drop")
				|| requestLower.startsWith("store")) {
			return TYPE_UMLDL;
		} else if (requestLower.startsWith("grant")) {
			return TYPE_UMLCL;
		}

		return 0;
	}

	public String dispatch(String request) {

		int requestType = getRequestType(request);
		if (TYPE_UMLQL == requestType) {
			return dispatchUMLQL(request);
		} else if (TYPE_UMLML == requestType) {
			return dispatchUMLML(request);
		} else if (TYPE_UMLDL == requestType) {
			return dispatchUMLDL(request);
		} else if (TYPE_UMLCL == requestType) {
			return dispatchUMLCL(request);
		}

		return null;
	}

	public String dispatchUMLQL(String request) {
		System.out.println("dispatching umlql");
		return "umlql";
	}

	public String dispatchUMLCL(String request) {
		System.out.println("dispatching umlcl");
		return "umlcl";
	}

	public String dispatchUMLDL(String request) {
		System.out.println("dispatching umldl");
		return "umldl";
	}

	public String dispatchUMLML(String request) {
		System.out.println("dispatching umlml");
		return "umlml";
	}
}
